package com.lidong.lowcode.model.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.lidong.lowcode.base.domain.baseconst.BaseConst;
import com.lidong.lowcode.base.domain.baseconst.TreeConst;
import com.lidong.lowcode.model.domain.BaseGroup;
import com.lidong.lowcode.model.mapper.BaseDataSourceMapper;
import com.lidong.lowcode.model.mapper.BaseGroupMapper;
import com.lidong.lowcode.model.mapper.ModelHelperMapper;
import com.lidong.lowcode.model.service.ModelTableService;
import com.lidong.lowcode.model.domain.BaseDataSource;
import com.lidong.lowcode.model.domain.modelconst.ModelConst;
import com.lidong.toolkit.constant.LogConst;
import com.lidong.toolkit.utils.UuidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Locale;
import java.util.Objects;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 5/4/2022 12:26 PM
 */
@Service
public class ModelTableServiceImpl implements ModelTableService {

    private static final Logger logger = LoggerFactory.getLogger(ModelTableServiceImpl.class);

    @Autowired
    private ModelHelperMapper modelHelperMapper;

    @Autowired
    private BaseDataSourceMapper datasourceMapper;

    @Autowired
    private BaseGroupMapper groupMapper;

    @Autowired
    private DruidDataSource datasource;

    /**
     * 创建数据源的表
     */
    @Override
    @Transactional
    public void createDataSourceTable() {
        try {
            modelHelperMapper.createDataSourceTable();
            // 插入内置数据源和内置分组
            String dbType = datasource.getDbType();
            String password = datasource.getPassword();
            String username = datasource.getUsername();
            String url = datasource.getUrl();
            BaseDataSource baseDataSource = new BaseDataSource();
            baseDataSource.setDatasourceCode(BaseConst.CURING_CODE + dbType);
            baseDataSource.setDatasourceName(BaseConst.CURING_NAME + dbType);
            baseDataSource.setDatasourceType(dbType.toUpperCase(Locale.ROOT));
            baseDataSource.setGroupCode(BaseConst.CURING_GROUP_CODE);
            baseDataSource.setGroupName(BaseConst.CURING_GROUP_NAME);
            baseDataSource.setUrl(url);
            baseDataSource.setInstanceName(username);
            baseDataSource.setPassword(password);
            BaseDataSource tempDataSource = datasourceMapper.getBaseDataSource(baseDataSource);
            if (Objects.isNull(tempDataSource)) {
                datasourceMapper.insertSelective(baseDataSource);
                BaseGroup baseGroup = new BaseGroup();
                baseGroup.setGroupCode(BaseConst.CURING_GROUP_CODE);
                baseGroup.setGroupName(BaseConst.CURING_GROUP_NAME);
                baseGroup.setType(ModelConst.DATASOURCE_CODE);
                baseGroup.setParentId(TreeConst.ROOT_CODE);
                BaseGroup tempGroup = groupMapper.getBaseGroup(baseGroup);
                if (Objects.isNull(tempGroup)) {
                    baseGroup.setId(UuidUtils.randomUuid());
                    groupMapper.insert(baseGroup);
                }
            }
        } catch (Exception e) {
            logger.error(LogConst.ERROR, e);
            //只要抛异常则回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
}
